home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / DefaultSingleSelectionMode.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  3.2 KB  |  116 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DefaultSingleSelectionModel.java    1.21 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package javax.swing;
  16.  
  17. import javax.swing.event.*;
  18. import java.io.Serializable;
  19.  
  20. /**
  21.  * A generic implementation of SingleSelectionModel.
  22.  * <p>
  23.  * <strong>Warning:</strong>
  24.  * Serialized objects of this class will not be compatible with 
  25.  * future Swing releases.  The current serialization support is appropriate
  26.  * for short term storage or RMI between applications running the same
  27.  * version of Swing.  A future release of Swing will provide support for
  28.  * long term persistence.
  29.  *
  30.  * @version 1.21 08/28/98
  31.  * @author Dave Moore
  32.  */
  33. public class DefaultSingleSelectionModel implements SingleSelectionModel, 
  34. Serializable {
  35.     /* Only one ModelChangeEvent is needed per model instance since the
  36.      * event's only (read-only) state is the source property.  The source
  37.      * of events generated here is always "this".
  38.      */
  39.     protected transient ChangeEvent changeEvent = null;
  40.     /** The collection of registered listeners */
  41.     protected EventListenerList listenerList = new EventListenerList();
  42.  
  43.     private int index = -1;
  44.  
  45.     // implements javax.swing.SingleSelectionModel
  46.     public int getSelectedIndex() {
  47.         return index;
  48.     }
  49.  
  50.     // implements javax.swing.SingleSelectionModel
  51.     public void setSelectedIndex(int index) {
  52.         if (this.index != index) {
  53.             this.index = index;
  54.         fireStateChanged();
  55.         }
  56.     }
  57.  
  58.     // implements javax.swing.SingleSelectionModel
  59.     public void clearSelection() {
  60.         setSelectedIndex(-1);
  61.     }
  62.  
  63.     // implements javax.swing.SingleSelectionModel
  64.     public boolean isSelected() {
  65.     boolean ret = false;
  66.     if (getSelectedIndex() != -1) {
  67.         ret = true;
  68.     }
  69.     return ret;
  70.     }
  71.  
  72.     /**
  73.      * Adds a ChangeListener to the button.
  74.      */
  75.     public void addChangeListener(ChangeListener l) {
  76.     listenerList.add(ChangeListener.class, l);
  77.     }
  78.     
  79.     /**
  80.      * Removes a ChangeListener from the button.
  81.      */
  82.     public void removeChangeListener(ChangeListener l) {
  83.     listenerList.remove(ChangeListener.class, l);
  84.     }
  85.     /*
  86.      * Notify all listeners that have registered interest for
  87.      * notification on this event type.  The event instance 
  88.      * is lazily created using the parameters passed into 
  89.      * the fire method.
  90.      * @see EventListenerList
  91.      */
  92.     protected void fireStateChanged() {
  93.     // Guaranteed to return a non-null array
  94.     Object[] listeners = listenerList.getListenerList();
  95.     // Process the listeners last to first, notifying
  96.     // those that are interested in this event
  97.     for (int i = listeners.length-2; i>=0; i-=2) {
  98.         if (listeners[i]==ChangeListener.class) {
  99.         // Lazily create the event:
  100.         if (changeEvent == null)
  101.             changeEvent = new ChangeEvent(this);
  102.         ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  103.         }           
  104.     }
  105.     }    
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.